<?php
namespace Home\Controller;
use Think\Controller;

class BackController extends CommonController {
	public  function index(){
		$this -> assign('title','账户回填');
		$userMod = M('user_type');
		//当前用户	
		$user_type = $userMod -> where("id = '%d' ",$_SESSION['users']['user_type'])->find();
		$data['id'] = array('egt',$_SESSION['users']['user_type']);
		$type = $userMod -> where($data) ->select();
		$this -> assign('is_out',$_SESSION['users']['is_out']);
		$this -> assign('type',$type);
		$this -> assign('user_type',$user_type);
		$logMod = M('account_log');		
		$user_update_log = $logMod -> field('change_desc,about_user_name,user_money2,change_time') ->where("user_id = '%d' and change_type='%s' ",$_SESSION['users']['user_id'],'101')->order('change_time desc')->select();
		
		$this -> assign('user_update_log',$user_update_log);


		$this -> display('');
	}
	//判断安全密码是否正确ajax
	public function backAjax(){
		if(md5(I('post.repassword'))==$_SESSION['users']['repassword']){
				if(I('post.backmoney')*100>$_SESSION['users']['user_money2']){
					echo '22';
				}else{
					echo '1';
				}
			
		}else{
			echo '2';
		}
	}
	public function backgrand(){
		$mod = M('user_type');
		$usermod = M('users');
		$logMod = M('account_log');
		$mod -> startTrans();
		$back_grand = $mod -> where('id = "%d" ',I('post.user_type'))->find();
		$_SESSION['users']['user_money2'] = $_SESSION['users']['user_money2'] - $back_grand['reg_amount'];
		$_SESSION['users']['is_out'] = 1;
		$_SESSION['users']['user_type'] = $back_grand['id'];
		$_SESSION['users']['reg_type'] = $back_grand['id'];
		$res1 = $usermod -> where('user_id = %s',$_SESSION['users']['user_id'])->save($_SESSION['users']);
		//封装日志
		$log['change_type'] = 101;
		$log['user_money2'] = -$back_grand['reg_amount'];
		$log['change_time'] = time();
		$log['change_desc'] = '账户回填'.$back_grand['name'];
		$log['about_user_name'] = $_SESSION['users']['user_id'];
		$log['user_id'] = $_SESSION['users']['user_id'];

		$res2 = $logMod -> data($log) -> add();
		if($res1>0&&$res2>0){
			$mod -> commit();
			$this -> success('回填成功',U('home/back/index'));
		}else{
			$mod -> rollback();
			$this -> error('回填失败',U('home/back/index'));
		}

	}

}